<?php

$peer = ProductCategoryAttendantPeer::getInstance();
$id = Zend_Controller_Front::getInstance()->getRequest()->getParam('id');
$select = $peer->select();
$select->where(ProductCategoryAttendantPeer::CATEGORY_ID, $id);
$rowset = $peer->fetchAll($select);

$count = $rowset->count();
echo "<label><b>Сопутствующие категории ($count):</b></label><br />";
foreach ($rowset as $row) {
	$category = ProductCategoryPeer::getInstance()->find($row->getAttendantId())->current();
	$url = $this->url(array('id' => $category->getId()));
	echo "
	<a href='$url'>{$category->getName()}</a>
	&nbsp;&nbsp;
	<a href='#' class='delete_attendant_category' type='attendant_category' row_id='{$category->getId()}'>[ x ]</a>
	<br />";
}


echo "<div class='container_attendant'>
<label>Добавить сопутствующую категорию(по имени) <a href='#' class='add_attendant_category' type='alike'>[ + ]</a></label>
";
$request = Zend_Controller_Front::getInstance()->getRequest();
$alikes = $request->getParam('attendant_category', array());
$first = 1;
foreach ($alikes as $alike) {
	if ($first) {
		$first = 0;
		continue;
	}

	if (!$alike) continue;
	echo
	"<div class='attendant_element'>
		<input type='text' autocomplete='off' name='attendant_category[]' class='attendant_category_suggest' type='attendant_category' value='$alike' />
		<div class='suggest_category_result'></div>
	</div>";
}

echo "
	<div class='attendant_category_element'>
		<input type='text' autocomplete='off' name='attendant_category[]' class='attendant_category_suggest' type='attendant'/>
		<div class='suggest_category_result'></div>
	</div>
</div>";


?>

<script type='text/javascript'>
		var $countOfPhotosAdded = 1;
		$(function() {
			$(".add_attendant_category").live("click", function() {
				var $self = $(this);
				var $label = $self.parent();
				var $element = $label.next();
				var $container = $label.parent();
				var $clone = $element.clone();
				$container.append($clone);
				$clone.find(':first').val('');

				return false;
			});


			$(".delete_attendant_category").live("click", function() {
				if (!confirm("Вы уверены, что хотите удалить сопутствующую категорию ?")) return false;
				$self = $(this);
				var $url = '/admin/product-category/delete_attendant_category/id/' + $self.attr('row_id') + '/type/' + $self.attr('type') + '/category_id/' + <?php echo $id; ?>;
				$.ajax({
					url: $url,
					type: 'post',
					dataType: 'json',
					success: function($data) {
						if ($data.result == 'success') {
							$self.prev().remove();
							$self.next().remove();
							$self.remove();
						} else {
							alert($data.message);
						}
					}
				});

				return false;
			});


			var suggestTimeout = null;
			$("input.attendant_category_suggest").live('keyup', function() {
				if (suggestTimeout != null) {
					clearTimeout(suggestTimeout);
					suggestTimeout = null;
				}

				var $self = $(this);
				var $value = $self.val();
				if (!$value) return;

				var $data = 'type=' + $self.attr('type') + '&name=' + $value;
				suggestTimeout = setTimeout(function() {
					var $url = '/admin/product-category/suggest_product_category';
					$.ajax({
						url: $url,
						type: 'post',
						data: $data,
						context: document.body,
						success: function(html) {
					        if (html == '') {
						        $(".suggest_category_result").hide();
						        blinkField($self);
						        return false;
					        }

							var $result = $self.next();
					        $result.html(html);
					        $result.show();
					    }
					});
				}, 650);

				return;
			});

			$("li.suggest_item").live('click', function() {
				var $self = $(this);

				var $result = $self.parent().parent();
				var $input = $result.prev();
				$input.val($self.text());
				$result.hide();
			});

		});




		</script>

		<style type='text/css'>
		.suggest_category_result {
			text-align: left;
			position: absolute;
			width: 300px;
			height: 200px;
			overflow-y: scroll;
			overflow-x: hidden;
			background-color: white;
			border: 2px black solid;
			display: none;
		}

		.suggest_item:hover, .suggest_item_street:hover, .suggest_item_city:hover {
			cursor: pointer;
			background-color: #d6ffd6;
		}
		</style>

